/**************************************************************************/
/*  EOSP                                                                  */
/*                                                                        */
/*  Copyright (c) 2013-2019                                               */
/*  All rights reserved.                                                  */
/*                                                                        */
/*  This project was developed by the SJQU Computer Department as part    */
/*  of the course 'Embedded Operation System Practice'.                   */
/*                                                                        */
/*  Redistribution and use in source and binary forms, with or without    */
/*  modification, are permitted provided that the following conditions    */
/*  are met:                                                              */
/*  1. Redistributions of source code must retain the above copyright     */
/*     notice, this list of conditions and the following disclaimer.      */
/*  2. Redistributions in binary form must reproduce the above copyright  */
/*     notice, this list of conditions and the following disclaimer in    */
/*     the documentation and/or other materials provided with the         */
/*     distribution.                                                      */
/*                                                                        */
/*  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''    */
/*  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED     */
/*  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       */
/*  PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR   */
/*  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,          */
/*  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES /INCLUDING, BUT NOT      */
/*  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF      */
/*  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION/ HOWEVER CAUSED AND   */
/*  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,    */
/*  OR TORT /INCLUDING NEGLIGENCE OR OTHERWISE/ ARISING IN ANY WAY OUT    */
/*  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF    */
/*  SUCH DAMAGE.                                                          */
/**************************************************************************/
#include "msg.h"
#include "app_log.h"
#include <stdint.h>

static void set_tail(app_msg_pt pt)
{
  uint8_t *crc = ((uint8_t*)pt + sizeof(*pt) + pt->dtsize + 1);
  *crc = APP_MSG_TAIL;
}

int get_size(app_msg_pt pt)
{
  return sizeof(*pt) + pt->dtsize + 2;
}

void app_send_msg(int id, char *data)
{
  uint8_t msg[16] = {0}, sendbuf[64] = {0};
  app_msg_pt pt = (app_msg_pt)msg;
  int i, size;
  
  pt->head = APP_MSG_HEAD;
  pt->id = (uint8_t)id;
  
  if (id == APP_ADC_MSG_ID) {
    pt->dtsize = sizeof(short);
    *(uint16_t*)(msg + sizeof(*pt)) = Rheostat_value();
    set_tail(pt);
  } else if (id == APP_HEARTBEAT_MSG_ID) {
    pt->dtsize = sizeof(char);
    set_tail(pt);
  } else if (id == APP_KEYBOARD_MSG_ID) {
    pt->dtsize = sizeof(char);
    msg[sizeof(*pt)] = *data;
    set_tail(pt);
  } else {
    return;
  }
  
  size = get_size(pt);
  for (i = 0; i < size; ++i) {
    sprintf((char*)sendbuf + i * 3, "%02X ", msg[i]);
  }
  dlog(">>> %s", sendbuf);
}
